import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { authRoutes } from './login'
import { userRoutes } from './user'
import { courseRoutes } from './course'
import HomeView from '@/views/HomeView.vue'
import { adminRoutes } from './admin'
import { counselorRoutes } from './counselor'
import { articleRoutes } from './article'
// 从正确的路由模块导入账户路由
import { accountRoutes } from './account'
import { orderRoutes } from './order'
// import { questionRoutes } from './questionclub'
import { useUserStore } from '@/stores/auth/user'

// 基础路由配置
const baseRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: { title: '首页' }
  }
]

// 合并所有路由配置
const routes: RouteRecordRaw[] = [
  ...baseRoutes,
  ...authRoutes,
  ...userRoutes,
  ...courseRoutes,
  ...adminRoutes,
  ...counselorRoutes,
  ...articleRoutes,
  ...accountRoutes,
  ...orderRoutes
  // ...questionRoutes
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫示例（可选）
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = to.meta.title as string
  }
  
  // 认证检查逻辑
  const userStore = useUserStore()
  if (to.meta?.requiresAuth && !userStore.isLoggedIn) {
    next('/login')
    return
  }
  
  next()
})

export default router